package com.ledim.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ledim.adapter.base.LedimBaseListAdapter;
import com.ledim.app.LeDimApplication;
import com.ledim.activity.base.LedimBaseListActivity;
import com.ledim.bean.FanDataBean;
import com.ledim.bean.base.BaseListResponse;
import com.letv.android.young.client.R;
import com.ledim.service.ApiService;
import com.ledim.service.LedimApiServiceFactory;
import com.ledim.service.LedimHttpObserver;
import com.ledim.adapter.LedimFollowUsersAdapter;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.LogUtil;
import com.ledim.utils.ToastUtil;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by ccx on 16/8/17.
 * 用户的关注和用户的粉丝列表
 */
public class LedimFollowUsersActivity extends LedimBaseListActivity
        implements View.OnClickListener {

    private String uid;
    private String relation;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //获取携带的数据
        uid = getIntent().getStringExtra(ConstantUtil.ID);
        relation = getIntent().getStringExtra(ConstantUtil.RELATION);
        title = getIntent().getStringExtra(ConstantUtil.TITLE);
        super.onCreate(savedInstanceState);

        //back按钮
        ImageView iv_back = findViewByIdExt(R.id.iv_back);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(this);

        //title
        TextView tv_title = findViewByIdExt(R.id.tv_title);
        tv_title.setText(title);

    }

    @Override
    protected LedimBaseListAdapter getAdapter() {
        return new LedimFollowUsersAdapter(this);
    }


    @Override
    protected boolean enablePullRefresh() {
        return false;
    }

    @Override
    protected void getDatas() {
        LedimApiServiceFactory.createService(ApiService.class)
                              .getFansListDatas(uid, relation,
                                     mLoadPage + 1, getPageSize(), LeDimApplication.getInstance()
                                                                                   .getAppVersionName())
                              .observeOn(AndroidSchedulers.mainThread())
                              .subscribeOn(Schedulers.io())
                              .subscribe(new LedimHttpObserver<BaseListResponse<FanDataBean>>() {

                                 @Override
                                 public void onCompleted() {
                                     super.onCompleted();
                                 }

                                 @Override
                                 public void onError(Throwable e) {
                                     super.onError(e);
                                     setServerDataError();
                                     ToastUtil.toast(e.getMessage());
                                     LogUtil.e(e.getMessage());

                                 }

                                 @Override
                                 public void onNext(BaseListResponse<FanDataBean> fanListResponse) {
                                     super.onNext(fanListResponse);
                                     if (fanListResponse != null && fanListResponse.success) {
                                         initTotalPages(fanListResponse.paged.total);
                                         setDatas(fanListResponse.data, fanListResponse.paged.more);
                                     } else {
                                         setServerDataError();
                                     }
                                 }
                             });

    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_follows_fans;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
